# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=chez-scheme
# XXX: Upgrades require rebuilding testing/idris2.
pkgver=9.5.8
pkgrel=4
pkgdesc="Cisco R6RS Scheme Compiler"
url="https://github.com/cisco/ChezScheme"
# upstream only supports x86/x86_64/armhf.
#  aarch64: https://github.com/cisco/ChezScheme/issues/545
#  riscv64: https://github.com/cisco/ChezScheme/issues/601
arch="x86 x86_64 armhf"
license="Apache-2.0"
makedepends="util-linux-dev ncurses-dev libx11-dev zlib-dev lz4-dev"
subpackages="$pkgname-doc"
source="https://github.com/cisco/ChezScheme/releases/download/v$pkgver/csv$pkgver.tar.gz
	0001-arm32le-only-use-cache-sysconf-constant-if-they-are-.patch
	0002-Don-t-rely-on-signed-integer-overflow-in-symhash-625.patch

	armhf-64bit-time_t.patch
	x86_64bit-time_t.patch"
builddir="$srcdir"/csv$pkgver

prepare() {
	default_prepare

	# Ensure we don't use vendored zlib/lz4 versions.
	rm -r zlib lz4
}

build() {
	case "$CARCH" in
		x86)    _host=ti3le   ;;
		x86_64) _host=ta6le   ;;
		armhf)  _host=arm32le ;;
	esac

	case "$CARCH" in
		x86)    conf="--threads" ;;
		x86_64) conf="--threads" ;;
		armhf)  conf=""          ;;
	esac

	# Use system version of zlib/lz4.
	export LDFLAGS="$LDFLAGS $(pkg-config --libs zlib) $(pkg-config --libs liblz4)"

	./configure \
		--installprefix=/usr \
		--temproot=$pkgdir \
		--installschemename="chez" \
		--installscriptname="chez-script" \
		--machine=$_host \
		$conf \
		ZLIB= LZ4=
	make
}

check() {
	make test
}

package() {
	make install DESTDIR="$pkgdir"

	# Remove pre-compiled example programs (reduces package size).
	rm "$pkgdir"/usr/lib/csv$pkgver/examples/*.so

	# Move examples to -doc subpackage.
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	mv "$pkgdir"/usr/lib/csv$pkgver/examples \
		"$pkgdir"/usr/share/doc/$pkgname/
}

sha512sums="
80a4e9f61ddb254bef1a249af1d32f918df88390946fbe6eeb62c3510c760bf899285be1aba70eda9b54bcb0c6fef3fe7deace648993cd9cece2d08cf0ade9c0  csv9.5.8.tar.gz
804d394fb17343f3a7999180e7c791743ecbabd2768d9cdcec3595cf01163a900073125c96cb7368fbe456377ec0911cc3f211231ff86ebf92e81d9451a430b7  0001-arm32le-only-use-cache-sysconf-constant-if-they-are-.patch
bd14222805e75f7c4bb7b2736e620b386c8cc1a360f99a0257349c225c7c998b0e6b3c85cd3d86cb293b156a9befe1fa6b7fa065c0f07059875980ce891fd927  0002-Don-t-rely-on-signed-integer-overflow-in-symhash-625.patch
216cd2f932e99d798ab5d01bfa3f811528b79462ce546204af9a071f8288d5e0f0eab5c0da5fc2adcb45d29162f0ee7d4ddc7dd1884b2382137ebb6063380f54  armhf-64bit-time_t.patch
e3ca092032fbc17e3bb946d5a8909665916941474efdd04b9a8918d5432ce7f3340b6de55658399be24428778b916a181ba8c9d34d29afc998f804a4ff3a2e69  x86_64bit-time_t.patch
"
